package pers.zc.activiti.service.impl;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.util.json.JSONObject;
import org.activiti.validation.ProcessValidator;
import org.activiti.validation.ProcessValidatorFactory;
import org.activiti.validation.ValidationError;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import pers.zc.activiti.service.AsyncAbstractClass;
import pers.zc.activiti.service.TestService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Annotion:
 * @ClassName: TestServiceImpl
 * @Author: Lin Fuyuan
 * @Date: 2019/9/18  15:56
 * @Version: 1.0
 */
@Service
public class TestServiceImpl implements TestService {

//    @Autowired
//    private HistoryService historyService;

    //    public Boolean startTest(String start) {
//        if (StringUtils.isNotBlank(start)) {
//            try {
//                Thread.sleep(6000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("start:" + start);
//            return true;
//        }
//        return false;
//    }
    @Override
    public Boolean startTest(String start) {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("普通类继承抽象类,该方法异步, 父类有@Service注解");
        System.out.println("startTest>>>>>:" + start+Thread.currentThread().getName());
        return true;
    }

    @Override
    public Boolean jobOne(String jobOne) {
        if (StringUtils.isNotBlank(jobOne)) {
            System.out.println("jobOne:" + jobOne);
            return true;
        }
        return false;
    }

    @Override
    public Boolean jobTwo(String jobTwo) {
        if (StringUtils.isNotBlank(jobTwo)) {
            System.out.println("jobTwo:" + jobTwo);
            return true;
        }
        return false;
    }

    @Override
    public Boolean endTest(String end) {
        if (StringUtils.isNotBlank(end)) {
            System.out.println("end:" + end);
            return true;
        }
        return false;
    }

    /**
     * 查询blob 数据内容
     *
     * @return
     */
    @Override
    public JSONObject selectBlob() {
        System.out.println("selectBlob");
        return null;
    }

//    /**
//     * 查询历史流程
//     *@param businessId 每个具体的业务流程
//     * @return
//     */
//    public String queryTest(String businessId){
//
//        HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(businessId).singleResult();
//        List<HistoricTaskInstance> taskInstances = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstance.getId()).orderByTaskCreateTime().asc().list();
//        for(HistoricTaskInstance taskInstance : taskInstances){
//            System.out.print("taskId:" + taskInstance.getId());
//            System.out.print("---taskName:" + taskInstance.getName());
//            System.out.print("---assignee:" + taskInstance.getAssignee());
//            System.out.print("---startTime:" + taskInstance.getStartTime());
//            System.out.print("---endTime:" + taskInstance.getEndTime());
//            System.out.println("---duration:" + taskInstance.getDurationInMillis());
//        }
//        return "查询成功";
//
//    }


    /**
     *
     */
    public static void main(String[] args) {
        //实例化BpmnModel对象
        BpmnModel bpmnModel = new BpmnModel();
        //开始节点的属性
        StartEvent startEvent = new StartEvent();
        startEvent.setId("start1shareniu");
        startEvent.setName("start1shareniu");
        //普通的UserTask节点
        UserTask userTask = new UserTask();
        userTask.setId("userTask1shareniu");
        userTask.setName("userTask1shareniu");
        //结束节点属性
        EndEvent endEvent = new EndEvent();
        endEvent.setId("endEventshareniu");
        endEvent.setName("endEventshareniu");
        //连线信息
        List<SequenceFlow> sequenceFlows = new ArrayList<SequenceFlow>();
        List<SequenceFlow> toEnd = new ArrayList<SequenceFlow>();
        SequenceFlow s1 = new SequenceFlow();
        s1.setId("starttouserTask");//设置id
        s1.setName("starttouserTask");//设置名称
        s1.setSourceRef("start1shareniu");//上个节点
        s1.setTargetRef("userTask1shareniu");//下个节点
        sequenceFlows.add(s1);
        SequenceFlow s2 = new SequenceFlow();
        s2.setId("userTasktoend");
        s2.setName("userTasktoend");
        s2.setSourceRef("userTask1shareniu");
        s2.setTargetRef("endEventshareniu");
        toEnd.add(s2);
        startEvent.setOutgoingFlows(sequenceFlows);
        userTask.setOutgoingFlows(toEnd);
        userTask.setIncomingFlows(sequenceFlows);
        endEvent.setIncomingFlows(toEnd);
        //Process对象 引擎
        Process process = new Process();
        process.setId("process1");
        process.addFlowElement(startEvent);
        process.addFlowElement(s1);
        process.addFlowElement(userTask);
        process.addFlowElement(s2);
        process.addFlowElement(endEvent);
        bpmnModel.addProcess(process);

        //bpmnModel 转换为标准的bpmn xml文件
        BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();

        byte[] convertToXML = bpmnXMLConverter.convertToXML(bpmnModel);

        String bytes = new String(convertToXML);

        System.out.println(bytes);
        testXml(bpmnModel);
    }

    /**
     * 验证xml文件是否可用
     *
     * @param bpmnModel
     */
    public static void testXml(BpmnModel bpmnModel) {
        //验证bpmnModel 是否是正确的bpmn xml文件

        ProcessValidatorFactory processValidatorFactory = new ProcessValidatorFactory();

        ProcessValidator defaultProcessValidator = processValidatorFactory.createDefaultProcessValidator();

//验证失败信息的封装ValidationError

        List<ValidationError> validate = defaultProcessValidator.validate(bpmnModel);

        System.out.println(validate.size());
    }

}
